var SettingHandler = function(){}

SettingHandler.prototype.listenAvatarUpdateEvent = function (){
    $("#avatar-input").on("change",function (){
        var image = this.files[0];
        var formData = new FormData();
        formData.append("image",image);

        forum.post({
            url:'/avatar/upload',
            data:formData,
            //jquery
            processData: false,
            contentType: false,
            success: function (result){
                if(result['code']===200){
                    var avatar = result['data']['avatar'];
                    var avatar_url = '/media/avatar/'+avatar;
                    $("#avatar-img").attr("src",avatar_url);
                }
            }
        })
    })
}

SettingHandler.prototype.listenSubmitEvent = function (){
    $("#submit-btn").on('click',function(event){
        event.preventDefault()

        var signature = $('#signagure-input').val();
        if (!signature) {
            alert("提交成功！");
            return;
        }
        if (signature && signature.length > 200){
            alert('签名长度必须在200字内');
            return;
        }
        forum.post({
            url:'/profile/edit',
            data:{signature},
            success:function (result){
                console.log(result)
                if(result['code']===200){
                    alert("提交成功！");
                }else{
                    alert(result['message']);
                }
            }
        })
    })
}

SettingHandler.prototype.run = function (){
    this.listenAvatarUpdateEvent();
    this.listenSubmitEvent();
}

$(function (){
    var handler = new SettingHandler();
    handler.run();
})